<template>
  <base-dialog
    :title="title + '角色'"
    ref="baseDialog"
    @handleClose="handleClose"
    @handleSubmit="handleSubmit"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
      <el-row>
        <el-col :span="24">
          <el-form-item label="角色名称" prop="roleName">
            <el-input
              v-model="form.roleName"
              placeholder="请输入角色名称"
              show-word-limit
              clearable
              :maxlength="20"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="角色编码" prop="roleCode">
            <el-input
              v-model="form.roleCode"
              placeholder="请输入角色编码"
              show-word-limit
              clearable
              :maxlength="20"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="角色描述" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          placeholder="请输入角色描述"
          show-word-limit
          :autosize="{ minRows: 2, maxRows: 12 }"
          clearable
          :maxlength="200"
        />
      </el-form-item>
    </el-form>
  </base-dialog>
</template>

<script>
import { add, edit } from "@/api/system/role"
export default {
  data() {
    return {
      title: "",
      form: {},
      rules: {
        roleName: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
        roleCode: [{ required: true, message: "请输入角色编码", trigger: "blur" }]
      }
    }
  },
  methods: {
    init(item, title) {
      this.title = title
      this.$refs.baseDialog.dialogVisible = true
      if (item) {
        this.form = JSON.parse(JSON.stringify(item))
      }
    },
    handleClose() {
      this.form = {}
      this.title = ""
      this.$refs.baseDialog.dialogVisible = false
    },

    // 提交表单
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.title === "编辑") {
            await edit(this.form)
            this.$message({
              type: "success",
              message: "角色编辑成功"
            })
            this.$emit("updatePage")
            this.handleClose()
          } else {
            await add(this.form)
            this.$message({
              type: "success",
              message: "角色新增成功"
            })
            this.$emit("updatePage")
            this.handleClose()
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .disUoloadSty .el-upload--picture-card {
  display: none; /* 上传按钮隐藏 */
}
.el-select {
  width: 100%;
}
</style>
